package com.aispeech.detector.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.util.DisplayMetrics;

import java.io.File;
import java.io.InputStream;

/**
 * @author rain  06.19 2019
 */

public class SystemInfoUtils {

    private final static String TAG = "SystemInfoUtils";

    public static float getTotalRam(Activity activity) {
        try {
            ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
            ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
            activityManager.getMemoryInfo(mi);
            return mi.totalMem / 1048576L;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static float getAvailableRam(Activity activity) {

        try {
            ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
            ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
            activityManager.getMemoryInfo(mi);
            return mi.availMem / 1048576L;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static float getTotalStorage() {

        try {
            StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
            float blockSize = statFs.getBlockSize();
            return statFs.getBlockCount() * blockSize / 1073741824;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static float getAvailableStorage() {
        try {
            StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
            float blockSize = statFs.getBlockSize();
            return statFs.getAvailableBlocks() * blockSize / 1073741824;
//        return statFs.getFreeBlocks() * blockSize;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static double getScreenSize(Activity activity) {
        DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
        float yInches = displayMetrics.heightPixels / displayMetrics.ydpi;
        float xInches = displayMetrics.widthPixels / displayMetrics.xdpi;
        return Math.sqrt(xInches * xInches + yInches * yInches);
    }

    public static int getCores() {
        return Runtime.getRuntime().availableProcessors();
    }

    public static float getClockMaxSpeed(String cpuIndex) {
        try {
            String[] args = {"/system/bin/cat", "/sys/devices/system/cpu/" + cpuIndex + "/cpufreq/cpuinfo_max_freq"};
            ProcessBuilder builder = new ProcessBuilder(args);
            // set working directory
            builder.directory(new File("/system/bin/"));
            builder.redirectErrorStream(true);
            Process process = null;
            process = builder.start();
            InputStream in = process.getInputStream();
            String temp = "";
            byte[] re = new byte[1024];
            while (in.read(re) != -1) {
                temp = temp + new String(re) + "\n";
            }
            in.close();
            return Float.parseFloat(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static float getClockMinSpeed(String cpuIndex) {
        try {
            String[] args = {"/system/bin/cat", "/sys/devices/system/cpu/" + cpuIndex + "/cpufreq/cpuinfo_min_freq"};
            ProcessBuilder builder = new ProcessBuilder(args);
            // set working directory
            builder.directory(new File("/system/bin/"));
            builder.redirectErrorStream(true);
            Process process = null;
            process = builder.start();
            InputStream in = process.getInputStream();
            String temp = "";
            byte[] re = new byte[1024];
            while (in.read(re) != -1) {
                temp = temp + new String(re) + "\n";
            }
            in.close();
            return Float.parseFloat(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }
}
